<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const posts = [
            { title:'I Love Javescript',author:'王总',id:1},
            { title:'CSS!',author:'张总',id:2},
            {title:'Dev tools webpack',author:'CEO'}
        ];

        const authors = [
            {name:'王总',hometown:'赣州',bio:'面向字节跳动编程'}
        ]

        function getPostById(id) {
            // const post = posts.find(post => post.id === id);
            // const {author} = post;
            // console.log(authors.find(item => item.name == author));
            
            return new Promise((resolve,reject) => {
                // resolve 封装耗时任务完成后执行
                // ewjiect 失败执行
                console.log('------');
                setTimeout(() => {
                    const post =posts.find(post => post.id == id);
                    console.log(post);
                    if(post) {
                        resolve(post);
                    } else {
                        let err = new Error('没有数据');
                        reject(err);
                    }
                },200)
            });
        }
        // console.log(getPostById(1));
        // console.log(JSON.stringify(posts));
        getPostById(10)
            .then((data) => {
                console.log(data,'//////');
            })
            .catch((err) => {
                console.log(err);
            })
    </script>
</body>
</html>